<?php

/**
 * Erra_System_AccessControl - Simple access control class
 */
class Erra_System_AccessControl {

    public static function getRole() {
        return isset($_SESSION['acl_role']) && is_array($_SESSION['acl_role']) ? $_SESSION['acl_role'] : array('default' => 0);
    }

    public static function is($roleName) {
        $role = self::getRole();
        if ($roleName == '_all') {
            return 1;
        }
        if (strstr($roleName, ':')) {
            $isOneOfThem = false;
            $roles = explode(':', $roleName);
            foreach ($roles as $a_role) {
                $isOneOfThem = isset($role[$a_role]) ? true : $isOneOfThem;
            }
            return $isOneOfThem;
        } else {
            return isset($role[$roleName]) ? 1 : 0;
        }
    }

    public static function setRole($key) {
        $all_access = config_get('roles');
        if (isset($all_access[$key])) {
            $_SESSION['acl_role'] = array($key => $all_access[$key]);
        }
        if ($key == 'default') {
            $_SESSION['acl_role'] = array('default' => 0);
        }
    }

}

?>
